package com.hongxun.design.ui.statisticsmap.pieview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.hongxun.design.R;
import com.hongxun.design.ui.piechart.ScreenUtils;
import com.hongxun.design.utils.DensityUtil;

import java.util.List;

/**
 * 自定义组件：饼状图
 * Created by hanj on 14-12-29.
 */
public class PieChartView extends View {
    private int screenW = -1, screenH = -1;
    /**
     * The paint to draw text, pie and line.
     */
    private Paint textPaint, piePaint, linePaint;

    /**
     * The center and the radius of the pie.
     */
    private int pieCenterX, pieCenterY, pieRadius;
    /**
     * The oval to draw the oval in.
     */
    private RectF pieOval;

    private float smallMargin;

    //private int[] mPieColors = new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW, Color.MAGENTA, Color.CYAN};

    private List<PieItemBean> mPieItems;
    private float totalValue;
    private Context context;

    public PieChartView(Context context) {
        super(context);
        this.context = context;

    }

    public PieChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

    }

    public PieChartView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;

    }

    private void init(Context context) {

        pieCenterX = screenW / 2;
        if (screenW > screenH) {
            pieRadius = screenH / 2;
        } else {
            pieRadius = screenW / 2;
        }

        pieCenterY = pieRadius;
        smallMargin = ScreenUtils.dp2px(context, 5);

        pieOval = new RectF();
        pieOval.left = pieCenterX - pieRadius;
        pieOval.top = pieCenterY - pieRadius;
        pieOval.right = pieCenterX + pieRadius;
        pieOval.bottom = pieCenterY + pieRadius;

        //The paint to draw text.
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(ScreenUtils.dp2px(context, 16));

        //The paint to draw circle.
        piePaint = new Paint();
        piePaint.setAntiAlias(true);
        piePaint.setStyle(Paint.Style.FILL);

        //The paint to draw line to show the concrete text
        linePaint = new Paint();
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(ScreenUtils.dp2px(context, 1));
        //r=new RectF();
    }


    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        screenW = measureWidth(widthMeasureSpec);
        screenH = measureHeight(heightMeasureSpec);

        //设置宽高


        setMeasuredDimension(screenW, screenH);
    }

    //根据xml的设定获取宽度
    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {

        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {

        }

        return specSize;
    }

    //根据xml的设定获取高度
    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        //wrap_content
        if (specMode == MeasureSpec.AT_MOST) {

        }
        //fill_parent或者精确值
        else if (specMode == MeasureSpec.EXACTLY) {

        }

        return specSize;
    }



    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        init(context);
        if (mPieItems != null && mPieItems.size() > 0) {
            float start = 0.0f;
            for (int i = 0; i < mPieItems.size(); i++) {
                //draw pie
                piePaint.setAntiAlias(true);
                piePaint.setStyle(Paint.Style.FILL);
                piePaint.setColor(context.getResources().getColor(mPieItems.get(i).getColor()));
                float sweep = mPieItems.get(i).getItemValue() / totalValue * 360;
                canvas.drawArc(pieOval, start, sweep, true, piePaint);
                piePaint.setStrokeWidth(4);
                piePaint.setStyle(Paint.Style.STROKE);
                piePaint.setColor(Color.WHITE);
                canvas.drawArc(pieOval, start, sweep, true, piePaint);
                start += sweep;
            }
        } else {
            textPaint.setTextSize(DensityUtil.dip2px(context, 20));
            textPaint.setColor(context.getResources().getColor(R.color.gray));
            canvas.drawText("暂无数据", screenW *2/ 5, screenH / 2, textPaint);
        }
    }

    public List<PieItemBean> getPieItems() {
        return mPieItems;
    }

    public void setPieItems(List<PieItemBean> pieItems) {
        this.mPieItems = pieItems;
        totalValue = 0;
        for (PieItemBean item : mPieItems) {
            totalValue += item.getItemValue();
        }
        invalidate();
    }

    private float getOffset(float radius) {
        int a = (int) (radius % 360 / 90);
        switch (a) {
            case 0:
                return radius;
            case 1:
                return 180 - radius;
            case 2:
                return radius - 180;
            case 3:
                return 360 - radius;
        }

        return radius;
    }


}
